#!/usr/bin/perl
$\=$/;
use lib 'mods';
use db_config;
use ExploitFarm_SQL;
use Util::Logger;
use submitter;
use Socket;


sub main
{
    my $db=new ExploitFarm_SQL($db_host,$db_port,$db_name,$db_login,$db_pass);
    $db->connect();
    $sub=new submitter();
    while(1)
    {
	
	print "Get flags";
        my $hflags=$db->get_flags();
        my $fids=[];
        my $flags=[];
        print "Flags: " . join '|', keys %$hflags;
        @fids=values %$hflags;
        @flags=keys %$hflags;
        my $statuses=$sub->submit_flags(\@flags);
        my $i=0;
        my $st={};
	print "Prepare statuses:";
        for(@$statuses)
        {
	    print ".";
            my $s=$$st{$_};
            $s=[] if (!defined($s));
            push(@$s,$fids[$i]);
            $$st{$_}=$s;
            $i++;
        }
	print "OK";
	print "Updating status";
        for(keys %$st)
        {
	    my $flgs=$$st{$_};
	    print "Status: $_ " . (join ' ',@$flgs) . "\n";
            $db->update_fstatus($_,$flgs);
        }
    }
}

main();
